package com.soft.javase.day10.practice.streamdemo2;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author yzg
 * @desc TODO
 * @date 2025/5/4
 */
public class StreamPractice {
    public static void main(String[] args) {
        // of() 方法
        Stream.of("A", "B", "C").forEach(System.out::println);

        // iterate() 方法
        Stream.iterate(0, n -> n + 2).limit(5).forEach(System.out::println);

        // generate() 方法
        Stream.generate(Math::random).limit(5).forEach(System.out::println);

        List<Integer> numbers = Arrays.asList(1,2,3,4,5);

        // findFirst() 方法
        Optional<Integer> first = numbers.stream().findFirst();
        System.out.println("findFirst(): " + first.orElse(0));

        // findAny() 方法
        Optional<Integer> any = numbers.stream().findAny();
        System.out.println("findAny(): " + any.orElse(0));

        // anyMatch() 方法
        boolean hasEven = numbers.stream().anyMatch(n -> n % 2 == 0);
        System.out.println("anyMatch(): " + hasEven);

        // distinct() 方法
        Stream.of(1, 2, 2, 3, 3, 3).distinct().forEach(System.out::println);

    }
}
